package org.blankie.cli;

import java.util.Arrays;
import java.util.Collection;
import org.blankie.model.BlkResource;
import org.blankie.model.fs.BlkFSDriver;
import org.blankie.model.fs.BlkFile;
import org.blankie.model.fs.BlkFolder;

public class Recurse {

    public static void main(String... args) throws Exception {
        Recurse recurse = new Recurse();
        recurse.recurse();
    }
    BlkFSDriver driver = new BlkFSDriver();

    void recurse() {
        Collection<BlkFolder> roots = driver.getRoots();

        for (BlkFolder root : roots) {
            System.out.println(String.format("Recursing root : %s", root.getName()));
            recurse(0, root);
        }
    }
    
    void recurse(int level, BlkResource<?> resource) {
        char[] indent = new char[level];
        Arrays.fill(indent, ' ');
        StringBuilder builder = new StringBuilder(new String(indent));
        
        if (driver.isLeaf(resource)) {
            builder.append(resource.getName());
        } else if (driver.isNode(resource)) {
            builder.append("+ ").append(resource.getName());
            
            for (BlkFolder folder : driver.getNodes(resource)) {
                recurse(level + 1, folder);
            }
            
            for (BlkFile file : driver.getLeaves(resource)) {
                recurse(level + 1, file);
            }
        }
        
        System.out.println(builder.toString());
    }
}
